﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Const;
using Entitas;
using UnityEngine;

public class EliminateHorizontalSystem : ReactiveSystem<GameEntity>
{
    private Contexts _contexts;
    public EliminateHorizontalSystem(Contexts context) : base(context.game)
    {
        _contexts = context;
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.ComponentsDestroy);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.hasComponentsItemEffectState
               && entity.componentsItemEffectState.EffectState == ItemEffectName.ELIMINATE_HORIZONTAL;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        var gameBoard = _contexts.game.componentsGameBoardComponents;
        GameEntity[] temp;
        
        foreach (GameEntity entity in entities)
        {
            for (int column = 0; column < gameBoard.columns; column++)
            {
                temp = _contexts.game
                    .GetEntitiesWithComponentsItemIndex(
                        new customData(column, entity.componentsItemIndex.index.y))
                    .ToArray();
                if (temp.Length == 1)
                {
                    temp[0].isComponentsDestroy = true;
                }
            }
        }
    }
}
